﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using EBEInventory_DAL;

public partial class Inventory_Items_Management_ItemInfo22 : System.Web.UI.Page
{
    InventoryEntities ctx = null;
    string parCatID;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {

            string opType = Server.UrlDecode(Request.QueryString["Operation"]);
            
            

            if (string.Compare(opType, "Register") == 0 || string.Compare(opType, "Edit") == 0 || string.Compare(opType, "Delete") == 0)
            {
                ViewState["Operation"] = btn_RegItem.Text = opType;
                parCatID = Server.UrlDecode(Request.QueryString["ParentID"]);
                ViewState["ParentCatID"] = parCatID;

                if (Session["selItem"] != null)
                {
                    ItemInfo itInfo = (ItemInfo)Session["selItem"];
                    HiddenID.Value = itInfo.ItemID.ToString();
                    txt_ItemName.Text = itInfo.ItemName;

                    txt_ItemUnit.Text = itInfo.Unit;

                    chk_HasAddInfo.Checked = itInfo.AdditionalInfo == 1 ? true : false;

                    if (chk_HasAddInfo.Checked)
                    {
                        txt_ItemSerialNo.Text = itInfo.itemsinfo_1.SerialNo;
                        txt_ItemModel.Text = itInfo.itemsinfo_1.Model;
                        txt_ItemPartNo.Text = itInfo.itemsinfo_1.PartNo;
                    }
                }
            }

            
           // if (parCatID != null)
              //  txt_ItemCode.Text = parCatID;
           // txt_ItemCode.Enabled = false;

        }

        else
        {
            ctx = InitConnection.Initialize();
        }
    }

    protected void btn_RegItem_Click(object sender, EventArgs e)
    {
       
            Dictionary<string, string> item = new Dictionary<string, string>();
            
        item["ItemCode"] = ViewState["ParentCatID"].ToString();
            item["Name"] = txt_ItemName.Text;
            
            item["Unit"] = txt_ItemUnit.Text;
            item["Serial"] = txt_ItemSerialNo.Text;
            item["Model"] = txt_ItemModel.Text;
            item["PartNo"] = txt_ItemPartNo.Text;
            item["HasAddInfo"] = chk_HasAddInfo.Checked.ToString();

            if (string.Compare(ViewState["Operation"].ToString(), "Register") == 0)
            {
                ItemsOps.AddItem(item, ctx);
            }

            if (string.Compare(ViewState["Operation"].ToString(), "Edit") == 0)
            {
                item["ID"] = HiddenID.Value;
                ItemsOps.EditItem(item, ctx);
            }

            if (string.Compare(ViewState["Operation"].ToString(), "Delete") == 0)
            {
                
                ItemsOps.DeleteItem(HiddenID.Value, ctx);
            }

        Response.Redirect("~/Inventory/Items Management//ListItems.aspx");

    }
}